<?php

namespace App\Admin\Controllers\Member;

use App\Models\Member\Price;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Show;
use Illuminate\Http\Request;

class PriceController extends Controller
{
    use ModelForm;
    protected $header = '会员等级管理';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header($this->header)
            ->description('Detail')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Price::class, function (Grid $grid) {

            $grid->model()->orderBy('lv','ASC');
            $grid->lv('等级');
            $grid->name('等级名称');
            $grid->default('默认会员等级')->switch([
                'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
            ]);

            $grid->term('购买类型')->display(function ($term) {
                return $term . '月';
            });

            $grid->price('购买价格');
            $grid->note('备注');

        });
    }

    public function detail($id)
    {
        $show = new Show(Price::findOrFail($id));

        $show->name('会员等级名称');
        $show->ratio('校币兑换比例');
        $show->term('购买类型')->display(function ($term){
            return $term . '月';
        });
        $show->price('购买价格');
        $show->note('备注');


        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Price::class, function (Form $form) {

            $form->text('name', '会员等级名称');
            $form->switch('default','默认会员等级')->states([
                'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
            ]);
            $form->text('ratio', '校币兑换比例');
            $form->select('term', '购买类型')->options(['12'=>'12月','24'=>'24月','36'=>'36月']);
            $form->decimal('price', '购买价格');
            $form->textarea('note', '备注');

            //$form->image('img','图片');

            $form->saving(function (Form $form) {
                if ($form->default == 'on'){
                    Price::where('default', '1')->update(['default' => 0]);
                }
            });

        });
    }

}
